package impl

import (
	"context"
	"fmt"
	"pd/modules/allowed_sign"
)

func (i *AsImpl) saveAllowed_sign(ctx context.Context, ins *allowed_sign.Allowed_sign) error {
	var err error
	// 开启事务
	tx, err := i.db.BeginTx(ctx, nil)
	if err != nil {
		return fmt.Errorf("start tx error,%s", err)
	}
	
	defer func() {
		if err != nil {
			if err := tx.Rollback(); err != nil {
				i.l.Errorf("rollback error,%s", err)
			}
		} else {
			if err := tx.Commit(); err != nil {
				i.l.Errorf("commit error,%s", err)
			}
		}
	}()

	r_stmt, err := tx.PrepareContext(ctx, InsertAllowed_signSQL)
	if err != nil {
		return err
	}
	defer r_stmt.Close()

	for i := range ins.Name {
		name := ins.Name[i]
		_, err = r_stmt.ExecContext(ctx,
			name, ins.Create_time, ins.Update_time, ins.Admin, ins.Remark, ins.Status,
		)
		if err != nil {
			return err
		}
	}

	return nil
}
